% lin.m 
% set up of dynamic equations from model

sstate % establishes model parameters and computes steady state 

% Variable definitions 

%    c  n  lambda  w  R  rk  y  e  i k      
%    1  2      3   4  5  6   7  8  9 10

ic=1; in=2; ilam=3; iw = 4; iR = 5; irk = 6; iy = 7; iq = 8; ii = 9;
ico  = 10; icg = 11; iva = 12; ipo = 13; ip = 14; ipof1 = 15; ipof2 = 16; 
ipof3 = 17; ipof4 = 18; iexpch = 19;
imu = 20; ihelp = 21; ioil = 22; idelpof = 23;

ik = 24; 
is = 25;
iilag = 26;
iclag = 27;
ipoflag = 28;

%  The exogenous variables are:

izoil = 1;  % shock to oil supply
iz = 2;    % shock to TFP

ny=28;
nx=2;

A=zeros(ny,ny);
B=zeros(ny,ny);
CF=zeros(ny,2*nx);


% 1- Household's FOC for consumption

B(1,ilam) = 1;  
B(1,ip) = 1;
B(1,ic) = sigma;   


% 2- Definition of price index for consumpption composite (of goods and oil)

B(2,ip) = p^(1-eps2); 
B(2,ipo) = -omega2*po^(1-eps2); 

% 3 - Agents' Demand for goods

B(3,ic) = 1;
B(3,ip) = eps2;
B(3,icg) = -1;

% 4 - Agents' Demand for oil

B(4,ic) = 1;
B(4,ip) = eps2;
B(4,ipo) = -eps2;
B(4,ico) = -1;

% 5 - Household's FOC for hours

B(5,iw) = 1;
B(5,ilam) = 1;
B(5,in) = -psi2;

% 6 - Household's FOC for bond holding

A(6,ilam) = 1;
B(6,ilam)= 1;
B(6,iR) = -1;


% 7 - Household's FOC for storage

if answinv == 1

B(7,ilam) = 1;
B(7,ipo) = 1;

A(7,ilam) = 1;
A(7,ipo) = 1;
A(7,is) = -(phi_inv*s_ss)/(alpha_inv*beta);

elseif answinv == 0
A(7,is) = 1;
end

% 8 - Household's FOC for capital


if answcost == 1;  % Adjust. costs on capital
A(8,ilam) = 1;
A(8,irk) = beta*rk;

B(8,ilam)= 1;

   A(8,ii) = beta*phicost*delta;
   A(8,ik) = -phicost*(1+beta*delta); 
   
   B(8,ik) = -phicost;
   
elseif answcost == 2; % Adjust. costs on investment (CEE)

B(8,imu)  = 1;

A(8,irk)  = beta*rk;
A(8,imu)  = beta*(1-delta);
A(8,ilam) = beta*rk;

 
end


% (foc for choice of i_t)

B(9,ilam) = 1;
B(9,imu) = -1;

if answcost == 2;
    A(9,ihelp) = beta*phi_CEE;
    B(9,ihelp) = phi_CEE;    
end


% Definition of "help"

B(10,ii) = -1;
B(10,iilag) = 1;
B(10,ihelp) = 1;


% Definition, Value Added (va)
helpva = (1-omega3)^(1/eps3) * (k)^((eps3-1)/eps3) + (omega3)^(1/eps3) * (n)^((eps3-1)/eps3);
B(11,iva) = -1;
B(11,ik) = ((1-omega3)^(1/eps3) * (k)^((eps3-1)/eps3))/helpva;
B(11,in) = ((omega3)^(1/eps3) * (n)^((eps3-1)/eps3))/helpva;


% Definition, Final good production Y
helpy = (1-omega1)^(1/eps1) * (va)^((eps1-1)/eps1) + (omega1)^(1/eps1) * (q)^((eps1-1)/eps1);
B(12,iy) = -1;
B(12,iva) = ((1-omega1)^(1/eps1) * (va)^((eps1-1)/eps1))/helpy;
B(12,iq) = ((omega1)^(1/eps1) * (q)^((eps1-1)/eps1))/helpy;
CF(12,iz) = 1;

% Input Demand for capital

B(13,irk) = -1;
B(13,iy) = 1/eps1;
B(13,iva) = 1/eps3- 1/eps1;
B(13,ik) = - 1/eps3;
CF(13,iz) = (eps1-1)/eps1;

% Input Demand for hours

B(14,iw) = -1;
B(14,iy) = 1/eps1;
B(14,iva) = 1/eps3- 1/eps1;
B(14,in) = - 1/eps3;
CF(14,iz) = (eps1-1)/eps1;

% Input Demand for oil

B(15,ipo) = -1;
B(15,iy) = 1/eps1;
B(15,iq) = - 1/eps1;

CF(15,iz) = (eps1-1)/eps1;

% definition of investment

A(16,ik) = 1;
B(16,ii) = delta;
B(16,ik) = 1-delta;


% market clearing, oil market

B(17,ioil) = oil_ss;
B(17,ico) = -co;
B(17,iq) = -q;
B(17,is) = alpha_inv*s_ss;
A(17,is) = s_ss*(1+kappa+phi_inv*s_ss);



% resource constraint:

B(18,icg) = cg;
B(18,ii) = delta*k;
B(18,iy) = -y;


%    B(18,ipo) = po*oil_ss;
%    B(18,ioil) = po*oil_ss;
  
B(19,ioil) = 1;
CF(19,izoil) = -1;    

% defining expected, ONE-period-ahead oil prices
% OR defining futures rate as expected future spot discounted by ratio of 
% m.u.c. So difference between expected value of future spot and 
% asset price of a futures contract

B(20,ipof1) = 1;
A(20,ipo) = 1;

if answfutures == 1;
A(20,ilam) = 1;
B(20,ilam) = 1;
end

% defining expected, TWO-period-ahead oil prices

B(21,ipof2) = 1;
A(21,ipof1) = 1;

if answfutures == 1;
A(21,ilam) = 1;
B(21,ilam) = 1;
end

% defining expected, THREE-period-ahead oil prices

B(22,ipof3) = 1;
A(22,ipof2) = 1;

if answfutures == 1;
A(22,ilam) = 1;
B(22,ilam) = 1;
end

% defining expected, FOUR-period-ahead oil prices

B(23,ipof4) = 1;
A(23,ipof3) = 1;

if answfutures == 1;
A(23,ilam) = 1;
B(23,ilam) = 1;
end

% define expected price increase in energy

A(24,ipo) = 1;

B(24,ipo) = 1;
B(24,iexpch) = 1;

%relate i_t t to i_t-1

B(25,ii) = 1;
A(25,iilag) = 1;

%relate c_t t to c_t-1

B(26,ic) = 1;
A(26,iclag) = 1;

%relate pof_t to pof_t-1

B(27,ipof4) = 1;
A(27,ipoflag) = 1;

% define change in futures

B(28,idelpof) = 1;
B(28,ipof4) = -1;
B(28,ipoflag) = 1;

nlead=1;

lpd=[ik is iilag iclag ipoflag];
np=max(size(lpd));



